<?php


namespace app\services\cupboard;


use app\common\CacheKey;
use app\models\cupboard\CupboardModel;
use app\services\BaseService;
use app\services\utils\RedisService;
use think\facade\Db;

class CupboardService extends BaseService
{
    protected static function initModel()
    {
        return new CupboardModel();
    }

    /**
     * 获取redis中距离内的某个运营商的所有商家 按距离由近到远排序
     * @param int $operatorId
     * @param string $longitude
     * @param string $latitude
     * @param int $radius
     * @return array
     */
    public static function getCupboardsByRedis(int $operatorId, string $longitude, string $latitude, int $radius): array
    {
        //过滤添加到redis中
        $redis = RedisService::instance();
        $key = sprintf(CacheKey::OPERATOR_CUPBOARD_LIST, $operatorId);
        $cupboardArray = $redis->georadius($key, $longitude, $latitude, $radius, 'km', ['WITHDIST', 'ASC']);
        if (!$cupboardArray) {
            return [];
        }
        return $cupboardArray;
    }

    /**
     * 获取柜机信息
     * @param $cupboardId
     * @return array|mixed|\think\db\BaseQuery|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCupboardInfo($cupboardId)
    {
        $info = self::initModel()::find($cupboardId);
        return $info;
    }
}